POJ-1979 Heavy Transportation (最大生成树)

描述

传送门:POJ-1979 Heavy Transportation

Background
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight.
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.

Problem
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo’s place) to crossing n (the customer’s place). You may assume that there is at least one path. All streets can be travelled in both directions.

输入描述

The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.

输出描述

The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.

示例

输入

1
2
3
4
5
1
3 3
1 2 3
1 3 4
2 3 5

输出

1
2
Scenario #1:
4

题解

题目大意

从城市1到城市N运送货物,有M条道路,每条道路都是双向的,每条道路都有它的最大载重量,问从城市1到城市N运送最多的重量是多少。

思路

这一题方法不唯一,生成树变形和最短路变形都能过这题,我当时使用kruscal水过了,有机会把几种写法都补齐。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<cstdio>
#include<queue>
const int MAXN = 1111, INF = 0x3f3f3f3f;
using namespace std;
int F[MAXN];
struct node{
int from, to, val;
}E[1000005];

bool cmp(node a, node b){
return a.val > b.val;
}

int Find(int x){
if(F[x] == x) return x;
else return Find(F[x]);
}

bool Merge(int x, int y){
x = Find(x);
y = Find(y);
if(x == y) return false;
else F[x] = y;
return true;
}

int main(){
int t, n, m;
scanf("%d", &t);
for(int d = 1; d <= t; d++){
scanf("%d %d", &n, &m);
for(int i = 0; i <= n; i++){
F[i] = i;
}
for(int i = 0; i < m; i++){
scanf("%d %d %d", &E[i].from, &E[i].to, &E[i].val);
}
sort(E, E+m, cmp);
int res;
for(int i = 0; i < m; i++){
if(Merge(E[i].from, E[i].to)){
if(Find(1) == Find(n)){
res = E[i].val;
break;
}
}
}
cout << "Scenario #" << d << ":" << endl;
cout << res << endl << endl;
}
}



/*
2
3 3
1 2 3
1 3 4
2 3 5
4 3
1 2 6
1 3 3
2 4 1
*/